<%@ page import="java.util.*,
                 com.hz.supermanage.dao.*,
                 com.hz.supermanage.bean.*" %>
<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" %>
<%!
    /**
     * 生成单位节点
     *
     * @param org
     * @return
     */
    public String createOAOrg(Hzsysorganization org, OrganizationDao dao) throws Exception {
        StringBuffer sb = new StringBuffer();
        sb.append("<node key=\"");
        sb.append(org.getDepid()).append("@org");
        sb.append("\" name=\"");
        sb.append(org.getDepname());
         /** 实际情况单位是必须有部门的，因此这里必须是true */
        Collection childs = dao.getChildNodes( String.valueOf(org.getDepid()) );
        if (childs.size() != 0) {
            sb.append("\" hasChild=\"true\"");
        } else {
            sb.append("\" hasChild=\"false\"");
        }
        if(org.getDeptype().equals("P")){
        	sb.append(" icon=\"/parts/images/img/pos.gif\" ");
        }else{
        	sb.append(" icon=\"/parts/images/img/dept.gif\" ");
        }
        
        sb.append(" url=\"").append(org.getUrl()).append("\">");
        sb.append("</node>");
        return sb.toString();
    }

%>
<%
	Hzsyspersonnel user = (Hzsyspersonnel) request.getSession().getAttribute("admin_user");
    //ConfigServer confServer = (ConfigServer) ConfigMgr.getBean("Server");
    /**
     * 操作逻辑，如果只有treeTypeId那么就读根，如果有提供具体
     * 的节点id的话，就读取该节点下的全部子节点
     */
    StringBuffer sb = new StringBuffer();
    try {
        String nodeId = request.getParameter("key")==null?"0":request.getParameter("key");
        OrganizationDao dao = new OrganizationDao();
        //OAUM um = OAUMFactory.getImpl();
        
        
        if (nodeId.equals("0")) { //显示用户所在单位与该单位下的部门
                /*OAOrganization organization = um.findOrgById(confServer.getRootOrgId());
        		  Collection orgChilds = um.findOrganizationByParentPK(organization.getPrimaryKey());
        			这里可以写获得当前用户的岗位id，然后找到直属总单位作为 根节点
        		*/
	        	List root = null;//根节点
	//    	  	根据其他情况查询根节点
	    		if(user.getPersonid()!=null && !"".equals(user.getPersonid())){
	    			//root = dao.
	    			root = dao.queryRootList();
	    		}else{
	    			root = dao.queryRootList();
	    		}
        		
        		for(int i = 0;i<root.size();i++){
	                //找其子单位
	                Hzsysorganization croot = (Hzsysorganization) root.get(i);
	        		Collection orgChilds = dao.getChildNodes( String.valueOf(croot.getDepid()) );
	        		System.out.println("croot.getDepid():"+croot.getDepid());
	                boolean isHasChild = orgChilds.size() > 0;
	                sb.append("<node key=\"");
	                sb.append(croot.getDepid() + "@org");
	                sb.append("\" name=\"");
	                sb.append(croot.getDepname());
	                sb.append("\" hasChild=\"").append(isHasChild).append("\"");
	                sb.append(" icon=\"/parts/images/img/org.gif\" url=\""+croot.getUrl()+"\">");
	
	                for (Iterator iterator1 = orgChilds.iterator(); iterator1.hasNext();) {
	                	Hzsysorganization childOrg = (Hzsysorganization) iterator1.next();
	                    sb.append(createOAOrg(childOrg, dao));
	                }
	
	                sb.append("</node>");
        		}
        } else { //岗位
             /** 如果显示中带有@表示点击的为单位否则为部门 */
            if (nodeId.indexOf("@") != -1) {
                String orgUuid = nodeId.split("@")[0];
                
                /**
                 * 列出子单位的节点
                 */
                Collection orgChilds = dao.getChildNodes(orgUuid);
                if (orgChilds != null) {
                    for (Iterator iterator = orgChilds.iterator(); iterator.hasNext();) {
                    	Hzsysorganization idChild = (Hzsysorganization) iterator.next();
                        sb.append(createOAOrg(idChild, dao));
                    }
                }

            } else { //如果为部门就显示部门的相关资料

            }



        }
    } catch (Exception e) {
    	e.printStackTrace();
        //throw new BusinessException("读取树节点失败", e);
    } 
%>
<?xml version="1.0" encoding="UTF-8"?>
<tree>
    <%=sb.toString()%>
</tree>
